package cn.com.blueInfo.bpm.person.repository.impl;

import cn.com.blueInfo.bpm.common.entity.BaseEntity;
import cn.com.blueInfo.bpm.common.enums.BaseDeleted;
import cn.com.blueInfo.bpm.common.repository.BaseRepository;
import cn.com.blueInfo.bpm.person.entity.UserIdentity;
import cn.com.blueInfo.bpm.person.enums.UserIdentityPrimaryOrgFlag;
import cn.com.blueInfo.bpm.person.enums.UserIdentityPrimaryStatus;
import cn.com.blueInfo.bpm.person.enums.UserIdentityStatus;
import cn.com.blueInfo.bpm.person.mapper.UserIdentityMapper;
import cn.com.blueInfo.bpm.person.repository.UserIdentityRepository;
import cn.com.blueInfo.bpm.person.request.UserIdentityDTO;
import cn.com.blueInfo.core.entity.CurrentLoginUserInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 员工身份信息表 数据服务实现类
 * @author suxch
 * @since 2025-09-01 19:28:09
 */
@Log4j2
@Service
public class UserIdentityRepositoryImpl
        extends ServiceImpl<UserIdentityMapper, UserIdentity>
        implements UserIdentityRepository, BaseRepository<UserIdentity, UserIdentityDTO.QueryDTO> {

    @Autowired
    private CurrentLoginUserInfo currentLoginUserInfo;

    /**
     * 提供获取 CurrentLoginUserInfo 的方法（由实现类提供，避免接口中注入）
     * @return CurrentLoginUserInfo
     */
    @Override
    public CurrentLoginUserInfo getCurrentLoginUserInfo() {
        return currentLoginUserInfo;
    }

    /**
     * 新增员工身份信息
     * @param userIdentity 员工身份信息参数
     */
    @Override
    public Integer createUserIdentity(UserIdentity userIdentity) {
        return sqlExecuteResult(save(userIdentity));
    }

    /**
     * 批量新增员工身份信息
     * @param userIdentityList 员工身份信息参数列表
     */
    @Override
    public Integer batchCreateUserIdentity(List<UserIdentity> userIdentityList) {
        return sqlExecuteResult(saveBatch(userIdentityList), userIdentityList.size());
    }

    /**
     * 删除员工身份信息
     * @param userIdentityId 员工身份信息ID
     */
    @Override
    public Integer deleteUserIdentity(Long userIdentityId) {
        UserIdentity userIdentity = new UserIdentity();
        userIdentity.setId(userIdentityId);
        userIdentity.setDeleted(BaseDeleted.DELETED);
        return sqlExecuteResult(removeById(userIdentity));
    }

    /**
     * 批量删除员工身份信息
     * @param userIdentityIdList 员工身份信息ID列表
     */
    @Override
    public Integer batchDeleteUserIdentity(List<Long> userIdentityIdList) {
        List<UserIdentity> userIdentityList = new ArrayList<>();
        for (Long userIdentityId : userIdentityIdList) {
            UserIdentity userIdentity = new UserIdentity();
            userIdentity.setId(userIdentityId);
            userIdentity.setDeleted(BaseDeleted.DELETED);
        }
        return sqlExecuteResult(removeBatchByIds(userIdentityList), userIdentityList.size());
    }

    /**
     * 更新部分员工身份信息
     * @param userIdentity 员工身份信息参数
     */
    @Override
    public Integer updateUserIdentityPartial(UserIdentity userIdentity) {
        validateUpdateId(userIdentity.getId());
        LambdaUpdateWrapper<UserIdentity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(UserIdentity::getId, userIdentity.getId());
        return sqlExecuteResult(update(updateWrapper));
    }

    /**
     * 更新全部员工身份信息
     * @param userIdentity 员工身份信息参数
     */
    @Override
    public Integer updateUserIdentity(UserIdentity userIdentity) {
        validateUpdateId(userIdentity.getId());
        return sqlExecuteResult(updateById(userIdentity));
    }

    /**
     * 查询一条员工身份信息
     * @param userIdentityId 员工身份信息ID
     */
    @Override
    public UserIdentity getUserIdentityById(Long userIdentityId) {
        LambdaQueryWrapper<UserIdentity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserIdentity::getId, userIdentityId);
        return getOne(queryWrapper);
    }

    /**
     * 分页查询员工身份信息
     * @param userIdentityQueryDTO 员工身份信息参数
     */
    @Override
    public Page<UserIdentity> getUserIdentityPage(UserIdentityDTO.QueryDTO userIdentityQueryDTO) {
        Page<UserIdentity> userIdentityPage = new Page<>(
                userIdentityQueryDTO.getPage(), userIdentityQueryDTO.getSize());
        LambdaQueryWrapper<UserIdentity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, userIdentityQueryDTO);

        BaseDeleted deleted = userIdentityQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeletePage(userIdentityPage, queryWrapper);
        } else {
            return page(userIdentityPage, queryWrapper);
        }
    }

    /**
     * 根据条件查询 员工身份信息 数量
     * @param userIdentityQueryDTO 员工身份信息参数
     */
    @Override
    public Long getUserIdentityCount(UserIdentityDTO.QueryDTO userIdentityQueryDTO) {
        LambdaQueryWrapper<UserIdentity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, userIdentityQueryDTO);

        BaseDeleted deleted = userIdentityQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeleteCount(queryWrapper);
        } else {
            return count(queryWrapper);
        }
    }

    /**
     * 根据条件查询 员工身份信息
     * @param userIdentityQueryDTO 员工身份信息参数
     */
    @Override
    public List<UserIdentity> getUserIdentityList(UserIdentityDTO.QueryDTO userIdentityQueryDTO) {
        LambdaQueryWrapper<UserIdentity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, userIdentityQueryDTO);

        BaseDeleted deleted = userIdentityQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeleteList(queryWrapper);
        } else {
            return list(queryWrapper);
        }
    }

    private void queryWrapper(LambdaQueryWrapper<UserIdentity> queryWrapper,
                              UserIdentityDTO.QueryDTO userIdentityQueryDTO) {

        Long userBaseId = userIdentityQueryDTO.getUserBaseId();
        queryWrapper.eq(ObjectUtils.isNotEmpty(userBaseId), UserIdentity::getUserBaseId, userBaseId);

        Long userAccountId = userIdentityQueryDTO.getUserAccountId();
        queryWrapper.eq(ObjectUtils.isNotEmpty(userAccountId), UserIdentity::getUserAccountId, userAccountId);

        Long orgId = userIdentityQueryDTO.getOrgId();
        queryWrapper.eq(ObjectUtils.isNotEmpty(orgId), UserIdentity::getOrgId, orgId);

        Long headShipId = userIdentityQueryDTO.getHeadShipId();
        queryWrapper.eq(ObjectUtils.isNotEmpty(headShipId), UserIdentity::getHeadShipId, headShipId);

        Long postId = userIdentityQueryDTO.getPostId();
        queryWrapper.eq(ObjectUtils.isNotEmpty(postId), UserIdentity::getPostId, postId);

        UserIdentityPrimaryStatus primaryStatus = userIdentityQueryDTO.getPrimaryStatus();
        queryWrapper.eq(ObjectUtils.isNotEmpty(primaryStatus), UserIdentity::getPrimaryStatus, primaryStatus);

        LocalDate startDate = userIdentityQueryDTO.getStartDate();
        queryWrapper.eq(ObjectUtils.isNotEmpty(startDate), UserIdentity::getStartDate, startDate);

        LocalDate endDate = userIdentityQueryDTO.getEndDate();
        queryWrapper.eq(ObjectUtils.isNotEmpty(endDate), UserIdentity::getEndDate, endDate);

        UserIdentityStatus identityStatus = userIdentityQueryDTO.getIdentityStatus();
        queryWrapper.eq(ObjectUtils.isNotEmpty(identityStatus), UserIdentity::getIdentityStatus, identityStatus);

        UserIdentityPrimaryOrgFlag primaryOrgFlag = userIdentityQueryDTO.getPrimaryOrgFlag();
        queryWrapper.eq(ObjectUtils.isNotEmpty(primaryOrgFlag), UserIdentity::getPrimaryOrgFlag, primaryOrgFlag);

        addQueryWrapper(queryWrapper, userIdentityQueryDTO, getCommonColumn());

    }

    private Map<String, SFunction<UserIdentity, ?>> getCommonColumn() {
        return BaseEntity.getCommonColumn(
                UserIdentity::getOrderNumber,
                UserIdentity::getTenantId,
                UserIdentity::getApplicationId,
                UserIdentity::getStatus,
                UserIdentity::getDeleted,
                UserIdentity::getDeleteUserId,
                UserIdentity::getDeleteTime,
                UserIdentity::getRemark,
                UserIdentity::getCreateUserId,
                UserIdentity::getCreateTime,
                UserIdentity::getUpdateUserId,
                UserIdentity::getUpdateTime,
                UserIdentity::getExtend1,
                UserIdentity::getExtend2,
                UserIdentity::getExtend3);
    }

}
